package com.thd.springboot.framework.cloud.feign.controller;

import com.thd.springboot.framework.utils.JacksonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * com.thd.springboot.framework.example.web.CloudController
 *
 * @author: wanglei62
 * @DATE: 2022/9/8 17:27
 **/
@Controller
@RequestMapping("/feign")
public class FeignController {



    @Autowired
    private DiscoveryClient discoveryClient;



    private Logger logger = LoggerFactory.getLogger(FeignController.class);


    /**
     * 查询所有注册在eureka中的服务
     * @return
     */
    @RequestMapping("/queryAllServicesInEureka")
    @ResponseBody
    public String queryAllServicesInEureka(){
//        List<Application> serviceList = EurekaServerContextHolder.getInstance().getServerContext().getRegistry().getSortedApplications();
//        String names = serviceList.stream().map( item -> item.getName()).collect(Collectors.joining(";"));


        List<List<ServiceInstance>> servicesList = new ArrayList<>();
        //获取服务名称
        List<String> serviceNames = discoveryClient.getServices();
        String names = serviceNames.stream().collect(Collectors.joining(";"));

        List<List<ServiceInstance>> serviceList = serviceNames.stream().map( item -> discoveryClient.getInstances(item)).collect(Collectors.toList());
        return JacksonUtil.objToJson(serviceList);
    }
}
